let express = require("express");
const bodyParser = require("body-parser");

// 路由
let reouter = express.Router();

// 加载模型数据库模块
let { audio_path } = require("../config/dbconfig");

//生成语音表格
reouter.post("/audioTable", bodyParser.json(), async (req, res, next) => {
  const result = await audio_path.findAndCountAll({ raw: true });
  res.status(200).send({
    meta: {
      msg: "成功",
      status: 200,
      data: result,
    },
  });
});

//删除生成语音
reouter.post("/audioDel", bodyParser.json(), async (req, res, next) => {
  // 接收传输数据
  const { id } = req.body;
  const result = await audio_path.destroy({
    where: { id: JSON.parse(id) },
  });

  res.status(200).send({
    meta: {
      code: 200,
      msg: "删除成功",
    },
  });
});
//更新生成语音
reouter.post("/audioUpdate", bodyParser.json(), async (req, res, next) => {
  // 接收传输数据
  const data = req.body;
  // 更新数据
  let result = await audio_path.update(data, {
    where: { id: JSON.parse(data.id) },
  });
  if (result[0] === 1) {
    res.status(200).send({
      code: 201,
      data: "更新成功",
    });
  }

  if (result[0] === 0) {
    res.status(201).send({
      data: "修改失败,没有数据更新请更新数据",
    });
  }
});

module.exports = reouter;
